Repository navigation
feat(dialect): add ON DUPLICATE KEY UPDATE - #408
alexskinner wants to merge 1 commit into
Conversation
e669d57 to
3a5e67e
Compare
fcf3170 to
8598266
Compare
Codecov Report❌ Patch coverage is
❌ Your patch check has failed because the patch coverage (86.43%) is below the target coverage (90.00%). You can increase the patch coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #408 +/- ##
==========================================
- Coverage 92.99% 92.91% -0.09%
==========================================
Files 275 275
Lines 39243 39707 +464
==========================================
+ Hits 36495 36892 +397
- Misses 2748 2815 +67
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
8598266 to
532333a
Compare
Add an optional ON DUPLICATE KEY UPDATE clause to ast::Insert, with the assignments and the row alias of INSERT ... AS alias. The generic parser lowers the clause only after SqlParser::with_on_duplicate_key_update(); otherwise it keeps rejecting it with the same error as before, so GenericSqlDialect is unchanged. The SQLite parser never produces it, and ON CONFLICT stays rejected everywhere. When a statement carries the clause, INSERT ... VALUES and INSERT ... SELECT update the row a primary key, or a UNIQUE key without NULLs, conflicts with instead of inserting. A row of the same statement counts as existing once written. Assignments read the existing row by column name and the proposed row through VALUES(col) or the row alias; an assignment may move the primary key, and moving onto an existing key fails as a duplicate. A row the assignments leave unchanged is not rewritten. Add SqlDialect::upsert_affected_rows(inserted, updated), defaulting to 1 for every row, which sets the affected rows of each inserted, changed and unchanged row. It is resolved once at bind time and kept on the insert operator; DynDialect forwards it to its current dialect. A unit test in src/dialect/mod.rs covers the default rejection, both forms of the proposed row and the hook. Co-authored-by: Alex Skinner <alexskinner@users.noreply.github.com>
532333a to
8e9feba
Compare
|
This feature is not merely limited in scope; it has a significant impact by introducing additional logic. Even if it achieves the desired functionality, it imposes a substantial maintenance burden—something I find unacceptable for an embedded database that prioritizes simplicity. The logic for I intend to reimplement this feature myself, or provide an extensible way for users to implement it on their own; I’m afraid I cannot accept the significant changes introduced by this PR. |
Part of #403.
Reworked on top of #410: the clause is now part of the KiteSQL AST and the dialect's parser decides whether to accept it.
What
ast::Insertgainson_duplicate_key_update: Option<ast::OnDuplicateKeyUpdate>, which holds the assignments and the optional row alias ofINSERT ... AS alias.SqlParser::with_on_duplicate_key_update()makes the generic parser lowerON DUPLICATE KEY UPDATEinto that field. Without it the clause is rejected with the same error as today (INSERT ON DUPLICATE KEY UPDATE is not supported), soGenericSqlDialectis unchanged and the existingtests/slt/insert.sltcase for that error is untouched. The SQLite parser never produces the clause, andON CONFLICTstays rejected everywhere.INSERT ... VALUESandINSERT ... SELECTupdate the row a primary key, or aUNIQUEkey with no NULLs in it, conflicts with instead of inserting. A row written earlier in the same statement counts as existing.VALUES(col)or the row alias, as inINSERT ... AS new ON DUPLICATE KEY UPDATE v = new.v. Column aliases on the row alias are rejected by the parser.Database::run, the statement's writes are then rolled back, as with any failed write.SqlDialecthook,upsert_affected_rows(inserted, updated) -> u64, defaulting to 1 for every row. It sets the affected-row count the statement reports for each inserted, changed and unchanged row. The binder resolves the three values once and stores them on the insert operator, so the executor never calls into the dialect.DynDialectforwards it.docs/features.md.Why
Upsert is the most common write pattern in applications written for MySQL-compatible databases.
ON DUPLICATE KEY UPDATEis that syntax, and the affected-row convention there is 1 for an insert, 2 for a change and 0 for a no-op. With #410 a dialect owns its syntax, so a dialect crate turns the clause on in its ownparseand picks the count through the hook, while core keeps one dialect-neutral upsert path. See #403.Testing
src/dialect/mod.rsruns the same upserts (primary-key conflict, unique-key conflict,VALUES(col), row alias, unchanged row) against the generic dialect, which still rejects the clause, and a test-only dialect whoseparsecallsSqlParser::with_on_duplicate_key_update(), with the default and with dialect-chosen affected rows.make test(656 passed, 2 ignored),make test-async(601 passed),make test-sltandmake test-slt-async(198 files each),make fmtand all fourmake clippyinvocations pass.Notes for review
src/dialect/mod.rs,src/parser/genericanddocs/features.mdas the session PR, so whichever lands second needs a trivial rebase.SqlDialecthook, following feat: KiteSQL AST and SQLite dialect #410's "the dialect rejects syntax it does not support" rule. Only the affected-row count, which is semantics, stays a hook.InsertOperatorgainson_duplicate_key_update: Option<OnDuplicateKeyUpdate>. For a plain insert it isNone, and the executor runs the same path as before.